package com.xtoad.ecms.baseinfo.service.impl;

import com.xtoad.ecms.baseinfo.converter.WebPageConverter;
import com.xtoad.ecms.baseinfo.dto.WebPageDTO;
import com.xtoad.ecms.baseinfo.model.WebPage;
import com.xtoad.ecms.baseinfo.repository.IWebPageRepository;
import com.xtoad.ecms.baseinfo.service.IWebPageService;
import com.xtoad.ecms.common.web.exception.BusinessException;
import com.xtoad.ecms.common.web.exception.ResultCodeEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * 页面类服务实现类
 *
 * @author xtoad
 * @date 2021/01/18
 */
@Service
public class WebPageServiceImpl implements IWebPageService {

    @Resource
    private IWebPageRepository webPageRepository;
    @Resource
    private WebPageConverter webPageConverter;

    /**
     * 根据id查询页面类
     *
     * @param id 页面类id
     * @return 查询结果
     */
    @Override
    public WebPageDTO getWebPageById(final Long id) {
        Optional<WebPage> webPage = webPageRepository.findById(id);
        if (webPage.isPresent()) {
            return webPageConverter.toDto(webPage.get());
        }

        throw new BusinessException(ResultCodeEnum.NOT_FOUND);
    }

    /**
     * 查询全部页面类，不分页
     *
     * @return 查询结果
     */
    @Override
    public List<WebPageDTO> getAllWebPage() {
        List<WebPage> allWebPages = this.webPageRepository.findAll();
        return webPageConverter.toDtoList(allWebPages);
    }

    /**
     * 新增页面类
     *
     * @param webPageDTO 保存对象
     * @return 新增结果
     */
    @Override
    public WebPageDTO insert(final WebPageDTO webPageDTO) {
        WebPage webPage = webPageConverter.toModel(webPageDTO);
        webPage = webPageRepository.save(webPage);

        return webPageConverter.toDto(webPage);
    }

    /**
     * 删除页面类
     *
     * @param id 删除对象id
     */
    @Override
    public void deleteById(final Long id) {
        webPageRepository.deleteById(id);
    }

    /**
     * 删除页面类
     *
     * @param webPageDTO 删除对象
     */
    @Override
    public void delete(final WebPageDTO webPageDTO) {
        WebPage webPage = webPageConverter.toModel(webPageDTO);
        webPageRepository.delete(webPage);
    }

    /**
     * 更新页面类
     *
     * @param webPageDTO 更新对象
     * @return 更新结果
     */
    @Override
    public WebPageDTO update(final WebPageDTO webPageDTO) {
        WebPage webPage = webPageConverter.toModel(webPageDTO);
        webPage = webPageRepository.save(webPage);

        return webPageConverter.toDto(webPage);
    }

    /**
     * 批量新增页面类
     *
     * @param webPageDTOList 新增对象
     * @return 新增结果
     */
    @Override
    public List<WebPageDTO> batchInsert(final List<WebPageDTO> webPageDTOList) {
        List<WebPage> webPages = webPageConverter.toModelList(webPageDTOList);
        webPages = webPageRepository.saveAll(webPages);

        return webPageConverter.toDtoList(webPages);
    }

    /**
     * 批量删除页面类
     *
     * @param webPageDTOList 删除对象
     */
    @Override
    public void batchDelete(final List<WebPageDTO> webPageDTOList) {
        List<WebPage> webPages = webPageConverter.toModelList(webPageDTOList);
        webPageRepository.deleteInBatch(webPages);
    }

    /**
     * 批量更新页面类
     *
     * @param webPageDTOList 更新对象
     * @return 更新结果
     */
    @Override
    public List<WebPageDTO> batchUpdate(final List<WebPageDTO> webPageDTOList) {
        List<WebPage> webPages = webPageConverter.toModelList(webPageDTOList);
        webPages = webPageRepository.saveAll(webPages);

        return webPageConverter.toDtoList(webPages);
    }

}
